package com.yixing.tech.wms.statistics.service;

import com.yixing.tech.common.CommonConstant;
import com.yixing.tech.wms.statistics.domain.entity.InventoryInitialization;
import com.yixing.tech.wms.statistics.domain.entity.InventoryOperationFlow;
import com.yixing.tech.wms.stock.out.StockOutConstants;
import com.yixing.tech.wms.stock.out.StockOutService;
import com.yixing.tech.wms.stock.out.domain.StockOutOperation;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InventoryOperationFlowStatisticsServiceImpl implements IInventoryOperationFlowStatisticsService {

    @Resource
    StockOutService stockOutService;

    @Override
    public void insertInventoryOperationFlowFromInventoryInitialization(InventoryInitialization inventoryInitialization) {
        // status == "finished"
        if(CommonConstant.InitStatus.finished.toString().equals(inventoryInitialization.getStatus())) {
            List<InventoryOperationFlow> list = new ArrayList<>();
            for(InventoryInitialization.BillDet det:  inventoryInitialization.getDtls()) {
                InventoryOperationFlow flow = new InventoryOperationFlow();
                list.add(flow);

                flow.setOrder("");
                flow.setTaskCode(inventoryInitialization.getWorkCode());
                flow.setTaskObjName("上架作业");
                flow.setFlowBizType(null);
                flow.setLocationCode(inventoryInitialization.getStockLocationCode());
                flow.setContainerCode(inventoryInitialization.getContainerCode());
                flow.setMaterialCode(det.getMaterialCode().getCode());
                flow.setBatchNo(det.getBatchNo());
                flow.setNum(det.getNum());

                flow.setCreateName(inventoryInitialization.getUpdateName());
                flow.setCreateTime(inventoryInitialization.getUpdateTime());
            }
            try {
                stockOutService.saveInventoryOperationFlowMulti(list);
            } catch (Exception e) {
                log.error("保存出入库流水抛出异常:{}", e);
            }
        }
    }

    @Override
    public void insertInventoryOperationFlowFromStockOutOperation(StockOutOperation operation) {
        if(operation.getActualQty() > 0) {
            String state = operation.getOperationState().toString();
            if(!StockOutConstants.OperationState.UNDELIVERED.equals(state) && !StockOutConstants.OperationState.FINISHED.equals(state))
                return;
            log.info("处理出入库流水信息:{}", operation);

            InventoryOperationFlow inventoryOperationFlow = new InventoryOperationFlow();
            inventoryOperationFlow.setOrder(operation.getBillDetId());
            inventoryOperationFlow.setTaskCode(operation.getOperationNo());
            inventoryOperationFlow.setTaskObjName("下架作业");
            inventoryOperationFlow.setFlowBizType(Document.parse(String.format("{id: ObjectId(\"%s\")}", StockOutConstants.FlowBizType.STOCK_INOUT)));
            inventoryOperationFlow.setLocationCode(operation.getLocCode());
            inventoryOperationFlow.setContainerCode(operation.getPodCode());
            inventoryOperationFlow.setMaterialCode(operation.getMat());
            inventoryOperationFlow.setBatchNo(operation.getBatchNo());
            inventoryOperationFlow.setNum(operation.getActualQty());

            inventoryOperationFlow.setCreateName(operation.getCreateName());
            inventoryOperationFlow.setCreateTime(operation.getUpdateTime());

            try {
                long mc = stockOutService.saveInventoryOperationFlow(inventoryOperationFlow);
                log.info("保存出入库流水:inventoryOperationFlow[{}],mc={}", inventoryOperationFlow, mc);
            } catch (Exception e) {
                log.error("保存出入库流水抛出异常:{}", e);
            }
        }
    }

    @Override
    public void syncStockOutOperation() throws Exception {
        List<StockOutOperation> operationList = stockOutService.queryStockOutOperationWithoutStatistics();
        for(StockOutOperation operation: operationList) {
            insertInventoryOperationFlowFromStockOutOperation(operation);
        }
    }
}
